package com.kkd.kkdapp.adapter;

import java.util.HashMap;
import java.util.List;

import org.apache.http.Header;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.TextureView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.activity.CheckMainUrlActivity;
import com.kkd.kkdapp.activity.ManageAwardActivity;
import com.kkd.kkdapp.activity.TwoFriendsActivity;
import com.kkd.kkdapp.http.entity.MyRecommendFriendDataList;
import com.kkd.kkdapp.http.entity.TwoFriendsData;
import com.kkd.kkdapp.http.entity.TwoFriendsDataList;
import com.kkd.kkdapp.http.manager.HttpCallBack;
import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.utils.LogUtil;
import com.kkd.kkdapp.utils.PreferenceUtils;
import com.kkd.kkdapp.utils.SystemUtils;

import com.kkd.kkdapp.view.RoundImageView;
import com.nostra13.universalimageloader.core.ImageLoader;

public class MyRecommendFriendAdapter extends BaseAdapter {

	private List<MyRecommendFriendDataList> dataList;
	private HashMap<Integer, Boolean> mHashMap = null;
	private int mPosition = -1;
	public HashMap<Integer, TwoFriendsData> marray = null;
	private TwoFriendsData mData = null;
	private HashMap<Integer, Boolean> twoHashMap = null;
	Toast mToast;
	private Context mContext;

	@SuppressLint("UseSparseArrays")
	public MyRecommendFriendAdapter(Context context,
			List<MyRecommendFriendDataList> data) {
		mContext = context;
		dataList = data;
		mHashMap = new HashMap<Integer, Boolean>();
		marray = new HashMap<Integer, TwoFriendsData>();
		twoHashMap = new HashMap<Integer, Boolean>();
	}

	class FriendOnClickListener implements OnClickListener {
		Intent intent;
		private String customerId;

		public FriendOnClickListener(String customerId) {
			this.customerId = customerId;
		}

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.award_ll:
				intent = new Intent(mContext, ManageAwardActivity.class);
				intent.putExtra("customerId", customerId);
				mContext.startActivity(intent);
				break;
			case R.id.bis_rl:
				intent = new Intent(mContext, TwoFriendsActivity.class);
				intent.putExtra("customerId", customerId);
				mContext.startActivity(intent);
				break;
			}

		}
	}

	private class ViewHodler {
		TextView num_tiyanjin;

		LinearLayout award; // 理财奖励
		RelativeLayout bis_rl; // 查看二度好友
		TextView userphone;
		TextView username;
		TextView time;
		TextView friend;
		ImageView advance;
		TextView headcount;
		TextView two_null;
		RoundImageView mHead;
		RoundImageView twohead;
		ViewGroup two_ll;
	}

	/**
	 * 二度好友
	 */
	public void quesetTwoFriends(String friendCustomerId,
			final ImageView advance, final TextView headcount,
			final TextView two_null, final RoundImageView twohead,
			final ViewGroup two_ll, final RelativeLayout bis_rl,
			final int position) {

	
		KkdHttpMagager.getInstance().getTwoFriendsManager(1, 5,  friendCustomerId,
				 TwoFriendsData.class, new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						TwoFriendsData data = (TwoFriendsData) responseBody;
						marray.put(position, data);
						twoImage(data, two_ll, bis_rl, position, advance,
								headcount, two_null, twohead);
					}

					@Override
					public void onFailure(int statusCode, String desc) {
						// showToast(desc);
					}
				});
	}

	public void twoImage(TwoFriendsData data, ViewGroup two_ll,
			RelativeLayout bis_rl, int position, ImageView advance,
			TextView headcount, TextView two_null, RoundImageView twohead) {

		List<TwoFriendsDataList> tdataList = data.getDataList();
		LogUtil.e("position", "" + position);
		LogUtil.e("tdataList.size()", "" + tdataList.size());

		if (tdataList.size() <= 0) {
			LogUtil.e("测试1", "aaaa");
			advance.setVisibility(View.GONE);
			headcount.setVisibility(View.GONE);
			two_null.setVisibility(View.VISIBLE);
			mHashMap.put(position, false);
			twohead.setVisibility(View.VISIBLE);

		} else if (tdataList.size() > 0) {
			LogUtil.e("测试2", "bbbbb");
			advance.setVisibility(View.VISIBLE);
			headcount.setVisibility(View.VISIBLE);
			two_null.setVisibility(View.GONE);
			twohead.setVisibility(View.GONE);
			headcount.setText(data.getTotalCount() + "人");

			mHashMap.put(position, true);
		}
		if (mHashMap.size() > 0) {
			if (mHashMap.get(position) != null) {
				if (mHashMap.get(position)) {
					if (tdataList.size() > 0) {
						bis_rl.setOnClickListener(new FriendOnClickListener(
								dataList.get(position).getCustomerId()));
						twoHashMap.put(position, true);
						for (int i = 0; i < tdataList.size(); i++) {
							RoundImageView mRoundImageView = new RoundImageView(
									mContext);
							int l24 = SystemUtils.dip2px(mContext, 8);
							mRoundImageView.setLayoutParams(new LayoutParams(
									l24, l24));

							// ImageCache.getInstance(context).displayImage(
							// mRoundImageView,
							// tdataList.get(i).getUserHeadUrl(),
							// R.drawable.myrecommendfriend_bis);
							final String url = tdataList.get(i)
									.getUserHeadUrl();
							if (TextUtils.isEmpty(url)) {
								mRoundImageView
										.setImageResource(R.drawable.myrecommendfriend_bis);
							} else {
								ImageLoader.getInstance().displayImage(
										tdataList.get(i).getUserHeadUrl(),
										mRoundImageView);
							}
							int l72 = SystemUtils.dip2px(mContext, 24);
							LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
									new ViewGroup.LayoutParams(l72, l72));
							if (i == 0) {
								layoutParams.leftMargin = 0;
							} else {
								layoutParams.leftMargin = SystemUtils.dip2px(
										mContext, 5);
							}
							layoutParams.rightMargin = SystemUtils.dip2px(
									mContext, 5);
							two_ll.addView(mRoundImageView, layoutParams);
							mRoundImageView
									.setOnClickListener(new OnClickListener() {

										@Override
										public void onClick(View v) {
											Intent intent = new Intent(
													mContext,
													CheckMainUrlActivity.class);
											intent.putExtra("mode", "1");
											intent.putExtra("url", url);
											mContext.startActivity(intent);
											((Activity) mContext)
													.overridePendingTransition(
															R.anim.zoomin,
															R.anim.zoomout);
										}
									});
						}

					}

				}
			}
		}
	}

	public void showToast(String hintLanguage) {
		if (mToast != null) {
			mToast.cancel();
			mToast = null;
		}
		mToast = Toast.makeText(mContext, hintLanguage, Toast.LENGTH_SHORT);
		mToast.show();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getCount()
	 */
	@Override
	public int getCount() {

		return dataList.size();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getItem(int)
	 */
	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return dataList.get(position);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getItemId(int)
	 */
	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getView(int, android.view.View,
	 * android.view.ViewGroup)
	 */
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHodler mViewHodler;
		TextView num_tiyanjin;
		LinearLayout award; // 理财奖励
		RelativeLayout bis_rl; // 查看二度好友
		TextView userphone;
		TextView username;
		TextView time;
		TextView friend;
		ImageView advance;
		TextView headcount;
		TextView two_null;
		RoundImageView mHead;
		RoundImageView twohead;
		ViewGroup two_ll;
		convertView = View.inflate(mContext, R.layout.my_recommend_friend_item,
				null);
		num_tiyanjin = (TextView) convertView.findViewById(R.id.num_tiyanjin);

		award = (LinearLayout) convertView.findViewById(R.id.award_ll);
		userphone = (TextView) convertView.findViewById(R.id.userphone);
		username = (TextView) convertView.findViewById(R.id.username);
		time = (TextView) convertView.findViewById(R.id.time);
		friend = (TextView) convertView.findViewById(R.id.friend);
		bis_rl = (RelativeLayout) convertView.findViewById(R.id.bis_rl);
		advance = (ImageView) convertView.findViewById(R.id.advance);
		headcount = (TextView) convertView.findViewById(R.id.headcount);
		two_null = (TextView) convertView.findViewById(R.id.two_null);
		mHead = (RoundImageView) convertView.findViewById(R.id.head);
		twohead = (RoundImageView) convertView.findViewById(R.id.twohead);
		two_ll = (ViewGroup) convertView.findViewById(R.id.two_ll);
		userphone.setTag(position);
		userphone.setText(dataList.get(position).getCustomerName());
		username.setTag(position);
		username.setText(dataList.get(position).getCustomerRealname());
		time.setTag(position);
		time.setText("注册时间：" + dataList.get(position).getAddDate());
		friend.setTag(position);
		num_tiyanjin.setTag(position);
		num_tiyanjin.setText(dataList.get(position).getInviteExperAmount()
				+ "元");
		if (dataList.get(position).getInviteExperStatus() == 1) {
			num_tiyanjin.setTextColor(Color.parseColor("#ff5e34"));
		} else {
			num_tiyanjin.setTextColor(Color.parseColor("#a5a5a5"));
		}
		mHead.setTag(position);
		mHead.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mContext, CheckMainUrlActivity.class);
				intent.putExtra("mode", "1");
				intent.putExtra("url", dataList.get(position).getUserHeadUrl());
				mContext.startActivity(intent);
				((Activity) mContext).overridePendingTransition(R.anim.zoomin,
						R.anim.zoomout);
			}
		});
		// ImageCache.getInstance(context).displayImage(mHead,
		// dataList.get(position).getUserHeadUrl(),
		// R.drawable.myrecommendfriend_head);
		ImageLoader.getInstance().displayImage(
				dataList.get(position).getUserHeadUrl(), mHead);
		friend.setText(dataList.get(position).getAmount() + "元");
		award.setTag(position);
		award.setOnClickListener(new FriendOnClickListener(dataList.get(
				position).getCustomerId()));
		bis_rl.setTag(position);
		advance.setTag(position);
		headcount.setTag(position);
		two_null.setTag(position);
		twohead.setTag(position);
		two_ll.setTag(position);
		mData = marray.get(position);
		LogUtil.e("position", "" + position);
		LogUtil.e("dataList.get(position).getCustomerId()",
				dataList.get(position).getCustomerId());
		if (mData == null) {
			quesetTwoFriends(dataList.get(position).getCustomerId(), advance,
					headcount, two_null, twohead, two_ll, bis_rl, position);
		} else {
			twoImage(mData, two_ll, bis_rl, position, advance, headcount,
					two_null, twohead);
		}
		return convertView;
	}

	public void clearTwoFriendData() {
		mData = null;
	}
}
